﻿using ExpressionTree.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace ExpressionTree
{
    /// <summary>
    /// expression 路由，也就是判断 expression 类型并交给正确的处理来处理
    /// </summary>
    public class ExpressionHandler
    {
        private BaseExpresstionHandler expresstionHandler = null;

        public ExpressionHandler(BaseExpresstionHandler expresstionHandler)
        {
            this.expresstionHandler = expresstionHandler;
        }

        /// <summary>
        /// 解析表达式
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public string Handler(Expression expression)
        {
            if (expression is BinaryExpression)
            {
                return expresstionHandler.BinaryExpressionHandler(expression);
            }
            else if (expression is ConstantExpression)
            {
                return expresstionHandler.ConstantExpressionHandler(expression);
            }
            else if (expression is MemberExpression)
            {
                return expresstionHandler.MemberExpressionHandler(expression);
            }
            else if (expression is MethodCallExpression)
            {
                return expresstionHandler.MethodCallExpressionHandler(expression);
            }
            else if (expression is NewExpression)
            {
                return expresstionHandler.NewExpressionHandler(expression);
            }
            else if (expression is LambdaExpression)
            {
                return expresstionHandler.LambdaExpressionHandler(expression);
            }
            return "";
        }
    }
}
